public class LinearList {
    private char[] elements; // 线性表元素存储数组
    private int size; // 线性表大小

    public LinearList() {
        elements = new char[10]; // 初始化数组大小为10
        size = 0;
    }

    public LinearList(String string) {
        elements = new char[10]; // 初始化数组大小为10
        size = 0;
        for (char c : string.toCharArray()) {
            if (c != ' ') {
                add(c);
            }
        }
    }


    public void add(char element) { // 添加元素到线性表
        if (!isValidLowercaseLetter(element)) {
            throw new IllegalArgumentException("元素必须为小写字母");
        }
        if (size == elements.length) {
            expandCapacity(); // 扩展数组容量
        }
        elements[size] = element;
        size++;
    }

    public boolean remove(char element) { // 从线性表中移除元素
        if (!isValidLowercaseLetter(element)) {
            throw new IllegalArgumentException("元素必须为小写字母");
        }
        for (int i = 0; i < size; i++) {
            if (elements[i] == element) {
                // 将后面的元素依次向前移动一位
                for (int j = i; j < size - 1; j++) {
                    elements[j] = elements[j + 1];
                }
                size--;
                return true;
            }
        }
        return false;
    }

    public boolean contains(char element) { // 判断线性表是否包含指定元素
        if (!isValidLowercaseLetter(element)) {
            throw new IllegalArgumentException("元素必须为小写字母");
        }
        for (int i = 0; i < size; i++) {
            if (elements[i] == element) {
                return true;
            }
        }
        return false;
    }

    public int size() { // 获取线性表长度
        return size;
    }

    public char[] getElements() { // 获取线性表所有元素
        char[] result = new char[size];
        System.arraycopy(elements, 0, result, 0, size);
        return result;
    }

    public LinearList union(LinearList otherList) { // 计算线性表的并集
        LinearList newList = new LinearList();
        for (int i = 0; i < size; i++) {
            newList.add(elements[i]);
        }
        for (int i = 0; i < otherList.size(); i++) {
            char element = otherList.getElements()[i];
            if (!newList.contains(element)) {
                newList.add(element);
            }
        }
        return newList;
    }

    public LinearList intersection(LinearList otherList) { // 计算线性表的交集
        LinearList newList = new LinearList();
        for (int i = 0; i < size; i++) {
            char element = elements[i];
            if (otherList.contains(element)) {
                newList.add(element);
            }
        }
        return newList;
    }

    public LinearList difference(LinearList otherList) { // 计算线性表的差集
        LinearList newList = new LinearList();
        for (int i = 0; i < size; i++) {
            char element = elements[i];
            if (!otherList.contains(element)) {
                newList.add(element);
            }
        }
        return newList;
    }

    private void expandCapacity() { // 扩展数组容量
        char[] newElements = new char[elements.length * 2];
        System.arraycopy(elements, 0, newElements, 0, size);
        elements = newElements;
    }

    private boolean isValidLowercaseLetter(char element) {
        return element >= 'a' && element <= 'z';
    }
}

